package com.ygp.dtp.core.convert;

import com.ygp.dtp.common.dto.ThreadPoolState;
import com.ygp.dtp.common.enums.RejectedTypeEnum;
import com.ygp.dtp.core.thread.DynamicThreadPoolExecutor;

import java.util.Objects;

/**
 * @description: 监控转换类
 * @motto: 代码源于生活，高于生活艺术
 * @author: zhouhengzhe
 * @date: 2022/8/8 16:34
 * @since 1.0
 **/
public class MetricsConverter {

    /**
     * 线程池状态转换参数
     *
     * @param executor 线程池
     * @return
     */
    public static ThreadPoolState convert(DynamicThreadPoolExecutor executor) {
        ThreadPoolState threadPoolState = null;
        if (Objects.nonNull(executor)) {
            threadPoolState = ThreadPoolState.builder()
                    .corePoolSize(executor.getCorePoolSize())
                    .maxPoolSize(executor.getMaximumPoolSize())
                    .queueType(executor.getQueue().getClass().getSimpleName())
                    .queueCapacity(executor.getQueue().size() + executor.getQueue().remainingCapacity())
                    .queueSize(executor.getQueue().size())
                    .queueRemainingCapacity(executor.getQueue().remainingCapacity())
                    .activeCount(executor.getActiveCount())
                    .taskCount(executor.getTaskCount())
                    .completedTaskCount(executor.getCompletedTaskCount())
                    .largestPoolSize(executor.getLargestPoolSize())
                    .poolSize(executor.getPoolSize())
                    .waitTaskCount(executor.getQueue().size())
                    .rejectHandlerName(RejectedTypeEnum.formatRejectName(executor.getRejectedExecutionHandler().getClass().getSimpleName()))
                    .dtpName(executor.getThreadPoolName())
                    .rejectCount(executor.getRejectCount())
                    .build();
        }
        return threadPoolState;
    }
}
